<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class article_model extends base_model {

	public function __construct() {
		parent::__construct();
	}

	//获取文章详情
	public function detail($id) {
		$sql = "SELECT * FROM {$this->tbp}article WHERE status=1 AND id='{$id}'";
		$row = $this->db->get_row($sql);
		return $row;
	}
	
	//分类列表
	public function categorys($parent_id=0) {
		$sql = "SELECT *, NULL as children FROM {$this->tbp}article_category WHERE status=1 AND parent_id='{$parent_id}' ORDER BY sort ASC, id ASC";
		$rs = $this->db->get_results($sql);
		if ($rs) {
			foreach ($rs as $k=>$g) {
				if ($g->parent_id) $rs[$k]->children = $this->categorys($g->parent_id);
			}
			$rs = add_domain_deep($rs, array('pic'));
		}
		return $rs;
	}

	//关联图片
	public function _pics($article_id, $limit=0) {
		$limit = $limit>0 ? "LIMIT {$limit}" : '';
		$sql = "SELECT pic FROM {$this->tbp}article_pic WHERE article_id='{$article_id}' ORDER BY id ASC {$limit}";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	//关联商品
	public function _goods($article_id) {
		$sql = "SELECT g.id, g.name, g.model, g.pic, g.price FROM {$this->tbp}article_goods ag LEFT JOIN {$this->tbp}goods g ON goods_id=g.id
			WHERE article_id='{$article_id}' ORDER BY ag.id ASC";
		$rs = $this->db->get_results($sql);
		return $rs;
	}

	//是否点赞
	public function _liked($article_id) {
		if (!$this->member_id) return 0;
		$sql = "SELECT count(*) FROM {$this->tbp}article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
		return intval($this->db->get_var($sql));
	}
}
